using System;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint.WebControls;

namespace Ratman.SharePoint.SPTwitter
{
    public class SPTwitterWebPart : WebPart
    {
        protected SPGridView _SPGridView = null;


        #region public properties

        private const string c_Email = null;
        private string m_Email = c_Email;

        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter")]
        [DefaultValue(c_Email)]
        [WebDisplayName("Email")]
        [WebDescription("Your Twitter email.")]
        public string Email
        {
            get { return m_Email; }
            set { m_Email = value; }
        }

        private const string c_Password = null;
        private string m_Password = c_Password;

        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter")]
        [DefaultValue(c_Password)]
        [WebDisplayName("Password")]
        [WebDescription("Your Twitter password.")]
        public string Password
        {
            get { return m_Password; }
            set { m_Password = value; }
        }

        #endregion


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                NetworkCredential cred = null;
                DataSet _DataSet = null;

                if (!string.IsNullOrEmpty(m_Email) && !string.IsNullOrEmpty(m_Password))
                {
                    cred = new NetworkCredential(m_Email, m_Password);
                    _DataSet = Http.Get("http://twitter.com/statuses/friends_timeline.xml", "", cred);
                }
                else
                {
                    _DataSet = Http.Get("http://twitter.com/statuses/public_timeline.xml", "", cred);
                }

                _SPGridView = new SPGridView();
                _SPGridView.EnableViewState = false;
                _SPGridView.AutoGenerateColumns = false;

                _SPGridView.Columns.Add(CreateBoundField("created_at", "created_at", "created_at"));
                _SPGridView.Columns.Add(CreateBoundField("id", "id", "id"));
                _SPGridView.Columns.Add(CreateBoundField("text", "text", "text"));
                _SPGridView.Columns.Add(CreateBoundField("source", "source", "source"));
                _SPGridView.Columns.Add(CreateBoundField("truncated", "truncated", "truncated"));
                //_SPGridView.Columns.Add(CreateBoundField("in_reply_to", "in_reply_to", "in_reply_to"));
                _SPGridView.Columns.Add(CreateBoundField("in_reply_to_user_id", "in_reply_to_user_id", "in_reply_to_user_id"));
                _SPGridView.Columns.Add(CreateBoundField("status_Id", "status_Id", "status_Id"));
                _SPGridView.Columns.Add(CreateBoundField("statuses_Id", "statuses_Id", "statuses_Id"));

                _SPGridView.DataSource = _DataSet.Tables[1];
                _SPGridView.DataBind();

                this.Controls.Add(_SPGridView);
            }
            catch (Exception ex)
            {
                Context.Trace.Warn("Error", "Error CreateChildControls: " + ex.Message);
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(ex.Message));
            }
        }


        #region helper functions

        private BoundField CreateBoundField(string dataField, string headerText, string sortExpression)
        {
            BoundField _BoundField = new BoundField();

            _BoundField.DataField = dataField;
            _BoundField.HeaderText = headerText;
            _BoundField.SortExpression = sortExpression;

            return _BoundField;
        }



        #endregion


    }
}
